package server

import (
	"errors"
	"github.com/doujunyu/gogo/sql_aid"
	"go_self/model"
	"time"
)

// ShopByApply 申请店铺
func ShopByApply(userId int64, title string) error {
	//检测是否已经有店铺
	findShopData, err := model.ShopByFind(userId)
	if err != nil || len(findShopData) != 0 {
		return errors.New("您已经有店铺，不可重复申请")
	}

	findShopApply, args := sql_aid.PgTable(model.TableByShopApply).Where("user_id = ?", userId).Where("status = ?", model.ShopApplyByStatusAwait).ToSql()
	findShopApplyData, err := sql_aid.DataToMap(model.PgSql.Query(findShopApply, args...))
	if err != nil || len(findShopApplyData) != 0 {
		return errors.New("您有待审核信息，请耐心等待")
	}

	ShopApplyInsert := make(map[string]interface{})
	ShopApplyInsert["user_id"] = userId
	ShopApplyInsert["title"] = title
	ShopApplyInsert["time_c"] = time.Now().Format(time.RFC3339)
	ShopApplyInsertSql, args := sql_aid.PgTable(model.TableByShopApply).InsertByMap(&ShopApplyInsert)
	_, err = model.PgSql.Exec(ShopApplyInsertSql, args...)
	if err != nil {
		return errors.New("申请失败")
	}
	return nil
}

// ShopByInfo 店铺详情
func ShopByInfo(shopId int64) map[string]interface{} {
	list, args := sql_aid.PgTable(model.TableByShop).Where("id = ?", shopId).ToSql()
	listData, err := sql_aid.DataToMap(model.PgSql.Query(list, args...))
	if err != nil || listData[0] == nil {
		return nil
	}
	return listData[0]
}
